<template>
  <div>
    <el-dialog :title="title" :visible="visible" @close="handleClose" width="600px">
      <el-form :model="formFrozen" label-position="right" ref="formFrozenRef">
        <el-form-item label="有效期" prop="expDate" :label-width="formLabelWidth">
          <el-date-picker :disabled="!formFrozen.expDate" :picker-options="pickerOptions" format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss" style="width:100%" v-model="formFrozen.expDate" type="datetime"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="复验期" prop="retestDate" :label-width="formLabelWidth">
          <el-date-picker :picker-options="pickerOptions" format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss" style="width:100%" v-model="formFrozen.retestDate" type="datetime"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    updateOutboundLimit
  } from '@/api/storeM/index.js'
  export default {
    props: {
      visible: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        formFrozen: {

        },
        formLabelWidth: '68px',
        title: '修改冻结库存',
        pickerOptions: {
          disabledDate(time) {
            // 禁用今天之前的日期
            return time.getTime() < Date.now() - 8.64e7; // 8.64e7 是一天的毫秒数
          }
        }
      };
    },
    methods: {
      //弹框
      openDiolog(row) {
        if (row) {
          this.formFrozen = {
            pkMaterial: row.pk_material || '',
            pkBatchcode: row.pk_batchcode || '',
            expDate: row.exp_date, // 使用格式化方法
            retestDate: row.retest_date,
            cwarehouseId: row.cwarehouse_id || '',
            cproductorid: row.pk_product || '',
            name: row.name || '',
            vbatchcode:row.vbatchcode || '',//批号
          };
        }
      },
      // 提取重置表单的逻辑
      handleClose() {
        // 通过事件通知父组件更新visible值，触发update:visible事件
        this.$emit('update:visible', false);
        this.$nextTick(() => {
          this.$refs.formFrozenRef.resetFields();
        })
      },
      //提交表单
      submit() {
        this.$refs.formFrozenRef.validate((valid) => {
          if (valid) {
            const apiMethod = updateOutboundLimit;
            // 调用相应的 API 方法
            apiMethod(this.formFrozen).then(res => {
              this.$message({
                message: res.msg, // 显示成功消息
                type: 'success',
              });
              this.$emit('successfun');
              // 提交成功后可以选择清空表单
              this.handleClose();
            }).catch(err => {
              this.$message({
                message: err?.msg || '操作失败', // 捕获错误信息
                type: 'error',
              });
            });
          } else {
            // 校验失败
            return false;
          }
        });
      }
    },
  };
</script>

<style></style>
